package mulot;

import level.Plateau;
import level.iDeplacement;
import level.iMulot;
import level.iTexture;
import level.mulotPerso;
import deplacements.fabriqueDeplacement;
import affichage.textureMulot;
import affichage.textureVide;
import outils.Direction;

public class Mulot implements iMulot {

	private int x , y;
	private int chute = 0;
	private boolean mort = false;
	private Direction d;
	private mulotPerso deleg;
	private Direction dOld;
	
	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	public void setDeleg(mulotPerso deleg) {
		this.deleg = deleg;
	}

	public boolean utile(){
		if(mort)
			return false;
		return true;
	}
	
	public Mulot(int x, int y, mulotPerso deleg){
		this.x=x;
		this.y=y;
		this.d = Direction.SUD;
		this.deleg = deleg;
		this.dOld = Direction.EST;
	}
	
	public Mulot(iMulot m){
		this.x = m.getX();
		this.y = m.getY();
		this.d = m.getD();
		this.deleg = m.getDeleg();
		this.dOld = m.getdOld();
	}
	
	public Direction getdOld() {
		return dOld;
	}

	public mulotPerso getDeleg() {
		return deleg;
	}

	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
	
	public boolean isMort() {
		return mort;
	}

	public Direction getD() {
		return d;
	}

	public int getChute() {
		return chute;
	}

	public void setMort(boolean mort) {
		this.mort = mort;
	}

	public void setChute(int chute) {
		this.chute = chute;
	}

	public void setD(Direction d) {
		this.d = d;
	}
	
	public void afficher(iTexture[][] tab, int i){
		this.deleg.setI(i);
		tab[x][y] = new textureMulot(mort, this.deleg.getI(), this.dOld);
	}
	
	public void effacer(iTexture[][] tab){
		tab[x][y] = new textureVide();
	}
	
	public void deplacer(Plateau p){
		int xMax = p.getPlateau().length;
		int yMax = p.getPlateau()[0].length;
		
		if(!mort){
			if(y<yMax-1 && p.getPlateau()[x][y+1].traversable()){
				d=Direction.SUD;
			}
			
			iDeplacement dep = fabriqueDeplacement.fabriquer(d);
			d = dep.newDirection(d, x, y, xMax, yMax, p.getPlateau(),this);
			
			if(d != Direction.NORD && d != Direction.SUD)
				dOld = d;
			
			if(p.getPlateau()[x+d.getDx()][y+d.getDy()].traversable()){
				x = x+d.getDx();
				y = y+d.getDy();
			}
			
			if(d == Direction.SUD)
				++chute;
			else
				chute = 0;
			if (chute >= 5){
				if(y < yMax-1){				
					if(!p.getPlateau()[x+d.getDx()][y+d.getDy()].traversable())
						mort = true;
				}
				else {
					mort = true;
				}
			}	
		}
	}

}
